package com.gduf.bbl.controller;

import java.util.Arrays;
import java.util.Map;


import com.gduf.bbl.common.utils.ImgUtil;
import com.gduf.bbl.common.utils.PageUtils;
import com.gduf.bbl.common.utils.R;
import com.gduf.bbl.entity.UserUpdateVo;
import com.gduf.bbl.entity.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gduf.bbl.entity.UserEntity;
import com.gduf.bbl.service.UserService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author xqw
 * @email 2041411595@qq.com
 * @date 2024-02-29 20:38:49
 */
@RestController
@RequestMapping("bbl/user")
@CrossOrigin(allowCredentials = "true")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;



    @PostMapping("/login")
    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "userImg",name = "userImg",value = "头像照片",required = true),
            @ApiImplicitParam(type = "Integer",name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(type = "Integer",name = "areaId",value = "校区id",required = true),
            @ApiImplicitParam(type = "String",name = "username",value = "用户名",required = true),
            @ApiImplicitParam(type = "String",name = "phone",value = "手机",required = true),
            @ApiImplicitParam(type = "String",name = "address",value = "详细地址",required = true),
            @ApiImplicitParam(type = "String",name = "school",value = "学校",required = true),

    })
    public R login(@RequestBody UserVo userVo){
        return userService.login(userVo);
    }

    @ApiOperation("修改用户个人信息")
    @PostMapping("/updateUser")
    public R updateUser(MultipartFile userImg,Integer userId,Integer areaId,String username,String phone,
                        String address,String school){

        final UserUpdateVo userUpdateVo = new UserUpdateVo();
        userUpdateVo.setUserId(userId);
        userUpdateVo.setAreaId(areaId);
        userUpdateVo.setUsername(username);
        userUpdateVo.setPhone(phone);
        userUpdateVo.setAddress(address);
        userUpdateVo.setSchool(school);

        //保存图片
        final String s = ImgUtil.uploadImg(userImg);
        userUpdateVo.setUserImg(s);

        return userService.updateUser(userUpdateVo);

    }


    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
